package com.guigu.base.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.base.entity.CoreOrg;
import com.guigu.base.service.CoreOrgService;
import com.guigu.tms.result.R;
import com.guigu.tms.result.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 组织 前端控制器
 * </p>
 *
 * @author TaoLong
 * @since 2021-11-23
 */
@RestController
@RequestMapping("/base/core-org")
@Api(tags = "组织管理")
public class CoreOrgController {
    @Autowired
    private CoreOrgService coreOrgService;


    @ApiOperation("组织增加")
    @ApiImplicitParam(name = "coreOrg", value = "组织对象")
    @PostMapping("/")
    public R add(@RequestBody CoreOrg coreOrg) {
        coreOrg.setCreateTime(new Date());
        coreOrgService.save(coreOrg);
        return new R(ResultCode.SUCCESS_CODE, "组织增加成功!", true);
    }
    /**
     * 分页显示组织信息
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation("分页显示组织")
    @GetMapping("/{pageNum}/{pageSize}")
    public R<Page<CoreOrg>> queryPage(@PathVariable String pageNum,
                                      @PathVariable String pageSize) {
        Page<CoreOrg> p = new Page<>();
        p.setCurrent(Long.parseLong(pageNum));
        p.setSize(Long.parseLong(pageSize));
        Page<CoreOrg> page = (Page<CoreOrg>) coreOrgService.page(p, null);
        return new R<>("2000", page);
    }

    /**
     * 根据id查找组织机构信息
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查找组织机构信息")
    @GetMapping("/{id}")
    public R<CoreOrg> findById(@PathVariable String id) {
        CoreOrg org = coreOrgService.getById(id);
        return new R<>("2000", org);
    }

    /**
     * 获取所有机构数据
     * @return
     */
    @ApiOperation("获取所有机构数据")
    @GetMapping("/queryAll")
    public R<List<CoreOrg>> queryAll() {
        List<CoreOrg> list = coreOrgService.list(null);
        return new R<>("2000", list);
    }
}

